package com.htc.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.htc.model.Product;;

public interface ProductMapper {

	@Select("SELECT product.*,classes.cname,(select count(buynum) from `order`,orderitem where `order`.oid=orderitem.oid and `order`.state='3' and pid=product.pid)buynum from product left join classes on product.cid=classes.cid  ${where}")
    List<Product> queryList(@Param("where")String where);
	
	@Select("SELECT product.*,classes.cname,(select count(buynum) from `order`,orderitem where `order`.oid=orderitem.oid and `order`.state='3' and pid=#{pid})buynum from product left join classes on product.cid=classes.cid  where product.pid=#{pid}")
    Product queryByPid(@Param("pid")int pid);
    
	@Select("SELECT count(*) from product  where cid=#{cid} or cid in(select cid from classes where pid=#{cid})")
    Long queryByClasses(@Param("cid")int cid);
	
	@Delete("delete from product where pid=#{id}")
    void del(int id);

	@Update("UPDATE `product`"
			+ " SET `pname`=#{pname}, `estoreprice`=#{estoreprice}, `markprice`=#{markprice},"
			+ " `cid`=#{cid}, `imgurl`=#{imgurl}, `description`=#{description},pnum=#{pnum}"
			+ " WHERE (`pid`=#{pid}); ")
    void update(Product product);
    
	@Insert("INSERT INTO `product` (`pname`, `estoreprice`, `markprice`, `pnum`, `cid`, `imgurl`, `description`,consumer_id)"
			+ " VALUES (#{pname}, #{estoreprice}, #{markprice}, #{pnum}, #{cid}, #{imgurl}, #{description},#{consumer_id});")
    void insert(Product product);
	
	@Update("update product set pnum=#{pnum} where pid=#{pid}")
	void updateStorage(@Param("pid")int pid,@Param("pnum")int pnum);
	
	@Update("update product set status=#{status} where pid=#{id}")
    void updateStatus(@Param("status")int status,@Param("id") int id);
	
	
	@Update("update product set pnum=pnum+#{pnum} where pid=#{pid}")
	void addStorage(@Param("pid")int pid,@Param("pnum")int pnum);
	
	
	@Select("select pnum>=#{pnum} from product where pid=#{pid}")
	Boolean checkStorage(@Param("pid")int pid,@Param("pnum")int pnum);
}
